﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Diagnostics;
using System.Threading;
using SBControlClient;


namespace Scripting_Language_Framework__2._0_
{
    public partial class Form1 : Form
    {
        static System.Timers.Timer exitProgram = new System.Timers.Timer(); //timer to close form  on closing
        
        //timers used to trigger implicit evnets
        static System.Timers.Timer stopTypingTimer = new System.Timers.Timer(); //timer to track when the user stops typing for an extended period of time
        static System.Timers.Timer elapsedTalkingTimer = new System.Timers.Timer(); //timer to track when the user types for an extended period of time
        static System.Timers.Timer elapsedResponseTimer = new System.Timers.Timer(); //timer to track when user (speaker's) turn is over and the response of the addressee is longer than a period of time
        static System.Timers.Timer stopTypingExendedTimer = new System.Timers.Timer(); // timer to track when user has stopped typing and alters the elapse talking timer
        
        //timers used to warn the user of an implicit about to occur
        static System.Timers.Timer stopTypingTimerWarning = new System.Timers.Timer(); //warns the user of a IET event (scan) is about to occur
        static System.Timers.Timer elapsedResponseTimerWarning = new System.Timers.Timer();//warns the user of a ERT event (concurrence) is about to occur
        static System.Timers.Timer stopTypingExendedTimerWarning = new System.Timers.Timer(); //warns the user that the ETT timer is about to be turned off leading to a IET event

        bool stopTypingTimer_handlerDefined = false; //variable to check if stopTypingTimer event handler has been added or removed
        bool stopTypingTimer_going = false; //variable to check if stopTypingTimer is running or not
        bool elapsedTalkingTimer_handlerDefined = false; //variable to check if elapsedTalkingTimer event handler has been added or removed 
        bool elapsedTalkingTimer_going = false; //variable to check if elapasedTalkingTimer is running or not
        bool elapsedResponseTimer_handlerDefined = false; //variable to check if elapsedReponseTimer event handler has been added or removed 
        bool elapsedResponseTimer_going = false; //variable to check if elapsedResponseTimer is running or not
        bool stopTypingExendedTimer_handlerDefined = false; //variable to check if stopTypingExtendedTimer event handler has been added or removed
        bool stopTypingExendedTimer_going = false; //variable to check if stopTypingExtendedTimer is running or not
        bool usersTurnOver = false;
        bool question = false; //variable to check if the text submitted contains a question
        bool conversation = false; //variable to check if the user has initiated a conversation with another person (ex. victim)
        bool fixate_initiated = false; //variable to check if the fixate gaze act has already been performed 
        bool fixate_occured = false; //variable to check to see if fixate at the end occured during the interaction
        bool concurrence_enabled = false; //variable to check if the concurrence gaze act is being performed  
        bool confusion_enabled = false; //variable to check if the implicit confusion gaze act is being performed 
        bool scan_enabled = false; //variable to check if the scan gaze act is being performed
        int DelBackspaceCount = 0; //variable to count how many times the user presses the backspace or delete key to detect implicit confusion
        int AlphaCount = 0; //variable to count how many characters the user types to make up a conversation statment to detect implict concurrence
        Random randomValue = new Random();
        int extendedInfo = 0; //variable to determine whether the information portion of the application is visible or not
        
        String inputWithTag; //appends entered text with appropriate tags
        String startTime = ""; //string that stores the time the user starts talking 
        String endTime = ""; //string that stores the time the user presses enter and submits the text
        
        //Variables used to perform strikethrough on deleted characters 
        Diff differance = new Diff(); //used to track deleted characters
        string oldtext = " "; //string prior to delete
        String text = ""; //string after delete
        bool backspaceflag = false; //tracks backspace press
        int[] oldcountbarray = new int[10000]; //text count array
        int[] oldbackspacecount = new int[10000]; //backspace count array
        int oldcountb = 0; //text count
        int BackspaceCount = 0; //determines Backspace count for the purpose of strike-through
        int counter = 0; //counter for the array, location in typed text
        bool firstime = true; //determins if this is the first input of the conversation to print the heading "User Input [timeStampHere]:" 
        bool performedstrikethrough = false; //keeps track of when delete occurs for strike-through
        bool probability = true;
        bool disableGazeActs = false; //detects if user has enabled or disabled gaze acts on Survivor Buddy
        bool SBstored = false; //detects if Survivor Buddy is in the resting position
        bool disabledTimers = false; //detects if timers are disabled
        String[] gazeActs = new String[100]; //array that consist of gaze acts occured
        int gaCounter = 0; //counts how many gaze acts occured
        String[] explicitBehaviors = new String[100]; //array that consist of how many behavior tags have been entered
        Queue reply = new Queue(); //queue to hold the replys from sending commands to the server client
        int start = 0; //start time
        double aversionProbability;
        double fixationProbability;
        String command = ""; //string to send commands to the server client

        adjustTextEntered adjustText = new adjustTextEntered();

        SBControlClient.SBControlClient sbc; //client needed to use the voice tool kit and communicate with the associated server
     
        public Form1()
        {
            InitializeComponent();
            this.Height = 690; //set height of form with all information shown
            StartPosition = FormStartPosition.CenterScreen; //position the form
            typeHereTB.Focus(); //put the cursor focus in the typeHere textbox

            Queue replyQueue = Queue.Synchronized(reply); //sync the replys 

            //set bounds and properties for the volume level silider
            volumeLevelSlider.Minimum = 0; 
            volumeLevelSlider.Maximum = 100;
            volumeLevelSlider.TickFrequency = 10;
            volumeLevelSlider.Value = 100;
            currentVolumeLevel.Text = volumeLevelSlider.Value.ToString(); //set label to display the value of the position of the volume level slider

            //set bounds and properties for the speech rate slider
            speechRateSlider.Minimum = -10;
            speechRateSlider.Maximum = 10;
            speechRateSlider.TickFrequency = 2;
            speechRateSlider.Value = 1;
            currentSpeechRate.Text = speechRateSlider.Value.ToString(); //set label to display the value of the position of the speech rate slider

            //set probabilities for gaze acts (aversion and fixation)
            aversionProbability = .73;
            fixationProbability = .7;

            stopTypingTimer.Interval = 18000; //18 seconds
            stopTypingTimer.AutoReset = false;
            stopTypingTimer.SynchronizingObject = this;
            stopTypingTimerWarning.Interval = 12000; //12 seconds
            stopTypingTimerWarning.AutoReset = false;
            stopTypingTimerWarning.SynchronizingObject = this; 

            elapsedResponseTimer.Interval = 10000;//10 seconds
            elapsedResponseTimer.AutoReset = true;
            elapsedResponseTimer.SynchronizingObject = this;
            elapsedResponseTimerWarning.Interval = 8000; //8 seconds
            elapsedResponseTimerWarning.AutoReset = false;
            elapsedResponseTimerWarning.SynchronizingObject = this; 

            elapsedTalkingTimer.Interval = 13000; //13 seconds 
            elapsedTalkingTimer.AutoReset = true;
            elapsedTalkingTimer.SynchronizingObject = this;
            
            stopTypingExendedTimer.Interval = 7000; //5 seconds
            stopTypingExendedTimer.AutoReset = false;
            stopTypingExendedTimer.SynchronizingObject = this;
            stopTypingExendedTimerWarning.Interval = 5000; //5 seconds
            stopTypingExendedTimerWarning.AutoReset = false;
            stopTypingExendedTimerWarning.SynchronizingObject = this;

            exitProgram.Interval = 30000;
            exitProgram.AutoReset = false;
            exitProgram.SynchronizingObject = this;
        }

        //connects to the sb client server and if successful get voices otherwise close form
        private void Form1_Shown(object sender, EventArgs e)
        {
            int pt = 3030;
            sbc = new SBControlClient.SBControlClient("127.0.0.1", pt);
            sbc.DataIn += new ReceivedDataEventHandler(sbc_DataIn);
            if (sbc.connect("protocolcoach"))
            {
                this.debugWindow.AppendText("Connected Successfully!\r\n");
                sbc.sendCommand("\"GET VOICES\",\"voices\"");
                Thread.Sleep(500);
                sbc.sendCommand("\"ACT\",\"openuid\",\"open\"");
                SBstored = false;
                startButton.Visible = false;
                storeButton.Visible = true;
                typeHereTB.Enabled = true;
            }
            else
            {
                Console.WriteLine("Error Connecting!");
                this.Hide();
                exitProgram.Enabled = true;
                exitProgram.Elapsed += ExitProgram;
            }
        }

        //Method to close the form and ensure Survivor Buddy is in the resting position
        private void Form1_Closing(object sender, FormClosingEventArgs e)
        {
            //if Survivor Buddy is not stored do so here
            if (SBstored == false)
            {
                e.Cancel = true;
                End();
                this.Hide();
            }
            
            //Now exit program gracefully (allowing enough time for recieved message to be returned then exit form
            exitProgram.Enabled = true;
            exitProgram.Elapsed += ExitProgram;
        }
/*
 * Methods for keyboard events 
 */
        /*
         * This method is called on KeyDown in the TypeHereTB and conducts actions based on what the user types. 
         */
        private void Start_Typing(object sender, KeyEventArgs e)
        {
            //if a question was previously asked reset the the variable question and AlphaCount to denote it's the user's turn again.
            if (usersTurnOver == true)
            {
                usersTurnOver = false;
                AlphaCount = 0;
            }
            
            //don't alow Enter or Return to be used without treating it the end of a turn
            e.SuppressKeyPress = (e.KeyCode == Keys.Enter | e.KeyCode == Keys.Return);
            
        /* 
         * If concurrence is enabled is enabled and the typeHereTB is empty then set concurrence to false and allow a fixate to occur in order to initate 
         * the start of the user turn. 
         */
            if (concurrence_enabled == true & typeHereTB.Text.Length == 0)
            {
                concurrence_enabled = false;
                fixate_initiated = false;
            }

            /*
             * If implicit confusion gaze act is enabled and the typeHereTB is empty or if implicit confused is enabled and AlphaCount is greater than ten 
             * (denoting a conversation is taking place) then disable implicit confusion gaze act and allow a fixate to take place.
             */
            if ((confusion_enabled == true & typeHereTB.Text.Length == 0) | (confusion_enabled == true & AlphaCount > 10))
            {
                confusion_enabled = false;
                debugWindow.Text += "Concurrence disabled\r\n";
                UpdateScrollOnTextBox(debugWindow);
                sbc.sendCommand("\"ACT\",\"homeuid\",\"home\"");
                fixate_initiated = false;
            }

            /*
             * If scan gaze act is enabled and the key pressed isn't backspace or delete key and the AlphaCount is greater than 1 allow a fixate gaze act 
             * to occur so that it denotes the start or continuation of a conversation. 
             */
            if (scan_enabled == true & e.KeyCode != Keys.Back & e.KeyCode != Keys.Delete & AlphaCount > 1)
            {
                scan_enabled = false;
                debugWindow.Text += "Scan disabled\r\n";
                UpdateScrollOnTextBox(debugWindow);
                sbc.sendCommand("\"ACT\",\"homeuid\",\"home\"");
                fixate_initiated = false;
            }

            /*
             * if the stopTypingExendedTimer timer is going disable the timer, and disable the stopTypingExendedTimerWarning timer since the coversation has 
             * started or continued.
             */
            if (stopTypingExendedTimer_going == true)
            {
                stopTypingExendedTimer.Enabled = false;
                stopTypingExendedTimerWarning.Enabled = false;
                stopTypingExendedTimer_going = false;
                Console.WriteLine("Stop Typing extended has been cancelled");
            }

            /*
             * remove the event handler from the stopTypingExendedTimer timer and the stopTypingExtendedTimerWarning timer since the timer has been disabled. 
             */
            if (stopTypingExendedTimer_handlerDefined == true)
            {
                stopTypingExendedTimer.Elapsed -= stopTypingExendedTimerSet;
                stopTypingExendedTimerWarning.Elapsed -= stopTypingExendedTimerWarningSet;
                stopTypingExendedTimer_handlerDefined = false;
            }

            /*
             * if elapsedResponseTimer is going then disable the elapseResponseTimer and the elapsedResponseTimerWarning since the conversation 
             * has started. Adjust fixate_initiated so that it doesn't occur again. 
             */
            if (elapsedResponseTimer_going == true)
            {
                elapsedResponseTimer.Enabled = false;
                elapsedResponseTimerWarning.Enabled = false;
                elapsedResponseTimer_going = false;
                fixate_initiated = false;
            }

            /*
             * Remove the elapsedResponseTimer and elapsedResponseTimerWarning event handlers since the elapseResponseTimer and elapseResponseTimerWarning 
             * has been disabled
             */
            if (elapsedResponseTimer_handlerDefined == true)
            {
                elapsedResponseTimer.Elapsed -= ConcurrenceGazeAct;
                elapsedResponseTimerWarning.Elapsed -= concurrenceBehaviorWarn;
                elapsedResponseTimer_handlerDefined = false;
            }

            //if the key pressed is backpace then set that to true and count the backspaces
            if (e.KeyCode == Keys.Back)
            {
                backspaceflag = true;
                BackspaceCount = BackspaceCount + 1;
                if (counter - 1 <= -1)
                {
                    //stop decreasing counter
                }
                else
                {
                    counter--;
                    oldcountbarray[counter] = oldcountb; //stores the length of text in TypeHereTB
                    oldbackspacecount[counter] = BackspaceCount; //store BackspaceCount in oldbackspacecount array
                }
            }

            /* 
             * If the key pressed is backspace or delete and the textbox which the user types is not empty increase the DelBackspaceCount 
             * to check for implicit confused. If DelBackspaceCount is greater than 5 then the user will need to type more consistently 
             * to create a larger AlphaCount
             */
            if (e.KeyCode == Keys.Back | e.KeyCode == Keys.Delete & typeHereTB.Text.Length > 0)
            {
                DelBackspaceCount++;
                if (DelBackspaceCount > 5)
                    AlphaCount = 0;
              
                if (DelBackspaceCount > 5 & elapsedTalkingTimer_going == true & disableGazeActs==false & usersTurnOver==false)
                    Update_label2("Confusion");
            }


            /*
             * checks to make sure keys pressed are for typing characters in a conversation not keys such as backpace, delete, left/right/up/down arrows, pageUp/pageDown, 
             * home, end, insert. 
             */
            if (e.KeyCode == Keys.Space)
            {
                oldcountbarray[counter] = oldcountb; //stores the length of text in TypeHereTB
                oldbackspacecount[counter] = BackspaceCount; //store BackspaceCount in oldbackspacecount array
                counter++;
            }

            if (e.KeyCode == Keys.A | e.KeyCode == Keys.B | e.KeyCode == Keys.C | e.KeyCode == Keys.D | e.KeyCode == Keys.E | 
                e.KeyCode == Keys.F | e.KeyCode == Keys.G | e.KeyCode == Keys.H | e.KeyCode == Keys.I | e.KeyCode == Keys.J |
                e.KeyCode == Keys.K | e.KeyCode == Keys.L | e.KeyCode == Keys.M | e.KeyCode == Keys.N | e.KeyCode == Keys.O |
                e.KeyCode == Keys.P | e.KeyCode == Keys.Q | e.KeyCode == Keys.R | e.KeyCode == Keys.S | e.KeyCode == Keys.T |
                e.KeyCode == Keys.U | e.KeyCode == Keys.V | e.KeyCode == Keys.W | e.KeyCode == Keys.X | e.KeyCode == Keys.Y |
                e.KeyCode == Keys.Z | e.KeyCode == Keys.ShiftKey | e.KeyCode == Keys.Shift)
                {
                    oldcountbarray[counter] = oldcountb; //stores the length of text in TypeHereTB
                    oldbackspacecount[counter] = BackspaceCount; //store BackspaceCount in oldbackspacecount array
                    counter++; //increment counter to the next posistion in the array
                    backspaceflag = false;  
                    AlphaCount++; //increase AlphaCount so that we keep track of the valid characters typed in a conversation

                //if AlphaCount is greater than 10 start to decrease the DelBackspaceCount so that there is less chance of an implicit confused gaze act
                    if (AlphaCount > 10 | AlphaCount < 25 & DelBackspaceCount != 0)
                    {
                        DelBackspaceCount--;
                    }

                    //if AlphaCount is greater than 25 then reset the DelBackspace count
                    if (AlphaCount > 25)
                    {
                        DelBackspaceCount = 0;
                        Update_label2("");
                    }

                    /* 
                     * If AlphaCount is greater than 25 and DelBackspace is less than 5 and the elapseTalkingTimer is going but the concurrence gaze act is not 
                     * going than put the warning that it will soon occur 
                     */
                    
                    /*if (AlphaCount > 15 & DelBackspaceCount < 5 & elapsedTalkingTimer_going == true & concurrence_enabled == false & disableGazeActs == false)
                        Update_label2("Congurence");
                     */
                }

                    /*
                     * If AlphaCount is 1 and fixate has not been enabled, the typeHereTB doesn't contain an empty space and confusion isn't enabled then perform 
                     * a fixate gaze act (to denote the start of the user's turn. 
                     */
                    if (AlphaCount == 1 & (typeHereTB.Text.Length + 1) == 1 & fixate_initiated == false & typeHereTB.Text != " " & confusion_enabled == false & disableGazeActs == false)
                    {
                        FixateGazeAct();
                    }

                    /*
                     * if AlphaCount is one character and it's the continuation of a conversation but a new text submission then add a new line and the intro 
                     * to the parsedTextTB. 
                     */
                    if ((typeHereTB.Text.Length+1)==1 & AlphaCount==1 & firstime == false & DelBackspaceCount==0)
                    {
                        parsedVersionTB.Text += "User Input[" + DateTime.Now.ToString("hh:mm:ss") + "]: ";
                    }           
            /*
             * if enter is pressed do the following actions 
             */
            if (e.KeyCode == Keys.Enter & typeHereTB.Text.Length > 0)
            {
                //if gaze acts are enabled do the following actions
                if (disableGazeActs == false)
                {
                    stopTypingTimer.Elapsed -= ScanGazeAct; //remove the event handler for the stopTypingTimer
                    stopTypingTimerWarning.Elapsed -= ScanGazeActWarn; //remove the event handler for the stopTypingTimerWarning 
                    stopTypingTimer_handlerDefined = false; //set this variable to false sine we removed the event handlers
                    stopTypingTimer.Enabled = false; //disable the stopTypingTimer  
                    stopTypingTimerWarning.Enabled = false; //disable the stopTypingTimerWarning since the user turn has ended
                    stopTypingTimer_going = false; //set this variable to false sense we disabled the timers

                    elapsedTalkingTimer.Elapsed -= ConcurrenceGazeAct; //remove the event handler for the elapaseTalkingTimer
                    elapsedTalkingTimer_handlerDefined = false; //set this variable to false since we removed the event handler
                    elapsedTalkingTimer.Enabled = false; //disable the elapseTalkingTimer since the user's turn is over in the conversation
                    elapsedTalkingTimer_going = false; //set this variable to false since elapseTalkingTimer has been disabled
                }

                    Update_label(""); //clear the gaze act warning label 
                    DelBackspaceCount = 0; //set DelBackspaceCount to 0 since user's turn in the conversation is over
                    AlphaCount = 0; //set AlphaCount to 0 since the user's turn in the conversation is over
                    String input = typeHereTB.Text; //save the text the user submitted to input String to be displayed 
                    typeHereTB.Text = "";
                    endTime = DateTime.Now.ToString("hh:mm:ss"); //get the time of when the text was submitted  to be displayed ??
                    String voiceToolkitString = ParseInput(input); //parse the input for question or explicit behaviors which will trigger different implicit or explicit events 
                    
                    //command = "\"ACT\",\"implicit\",\"aversion\""; //do the aversion gaze act before speaking  
                    //sbc.sendCommand(command); //send command
                    //Thread.Sleep(500); //then give some time for act to start
                    usersTurnOver = true;
                    
                    sbc.sendCommand(voiceToolkitString);
                    debugWindow.AppendText("\nSent: " + voiceToolkitString + "\r\n");
                    UpdateScrollOnTextBox(debugWindow);
                    Console.WriteLine("\nSent: " + voiceToolkitString);
                    if(String.Compare(voiceToolkitString,"")!=0)
                        parsedVersionTB.Text += "\r\n —————\r\n";
                    UpdateScrollOnRichTextBox(parsedVersionTB); //fix the verticle scroll to show the most recent gaze acts that occured 
                    UpdateScrollOnRichTextBox(gazeActsTB);

                    startTime = ""; //reset startTime so it can be set when the user starts typing again
                    endTime = ""; //reset endTime so it can be set when the user presses enter (denoting the end of the user's turn when they submit the text)
                }

            else if (e.KeyCode == Keys.Enter & typeHereTB.Text.Length == 0)
            {
                // if no text is in the typeHereTB do nothing 
            }

            else
            {
                //if gaze acts are enabled do the following things
                if (disableGazeActs == false)
                {
                    /*
                     * disables the stopTypingTimer since the user is typing
                     */
                    if (stopTypingTimer_going == true)
                    {
                        stopTypingTimer.Enabled = false;
                        stopTypingTimerWarning.Enabled = false;
                        stopTypingTimer_going = false;
                    }

                    /*
                     * remove the event handler from the stopTypingTimer timer and the stopTypingTimerWarning timer since the timer has been disabled. 
                     */
                    if (stopTypingTimer_handlerDefined == true)
                    {
                        stopTypingTimer.Elapsed -= ScanGazeAct;
                        stopTypingTimerWarning.Elapsed -= ScanGazeActWarn;
                        stopTypingTimer_handlerDefined = false;
                    }

                    /*
                     * Since the user is typing make sure that the elapsedTalkingTimer is going and clear the gaze act warning label 
                     */
                    if (elapsedTalkingTimer_going == false)
                    {
                        elapsedTalkingTimer.Enabled = true;
                        Update_label2("");
                        elapsedTalkingTimer_going = true;
                    }

                    /*
                     * Add the event handler for the concurrence gaze act to occur after a period of time
                     */
                    if (elapsedTalkingTimer_handlerDefined == false & fixate_initiated == true)
                    {
                        elapsedTalkingTimer.Elapsed += ConcurrenceGazeAct;
                        elapsedTalkingTimer_handlerDefined = true;
                    }

                    Console.WriteLine("User is typing"); //print that user is typing 

                    if (startTime.Length == 0)
                    {
                        startTime = DateTime.Now.ToString("hh:mm:ss"); //set the start time if it hasn't already be set. 
                    }
                }
            }
        }
        /*
         * Method for when text is added to the type here textbox in order to reflect that in the parsed version of input textbox 
         */
        private void updatetextbox1(object sender, EventArgs e)
        {
            oldtext = text; //get the text previously entered
            text = typeHereTB.Text; //get the text just entered
            if (String.Equals(text, oldtext)) //compare the length of the text
            {
                //if the same do nothing
            }
            else if (typeHereTB.Text.Length > 0 && backspaceflag == false) //if the length in the typing textbox is greater than 0 
            {
                if (firstime) //starting a statement 
                {
                    UpdateScrollOnRichTextBox(parsedVersionTB);
                    parsedVersionTB.Text += "User Input[" + DateTime.Now.ToString("hh:mm:ss") + "]:" + text; //print the header and the first letter typed
                    firstime = false; //set this to false so it doesn't print header again
                }
                else if (performedstrikethrough)
                {
                    parsedVersionTB.Text += text.Substring(text.Length - 1, 1); //add the new letter to the parsedVersionTB
                    oldcountbarray[counter] = oldcountb; //stores the length of text in TypeHereTB
                    oldbackspacecount[counter] = BackspaceCount; //store BackspaceCount in oldbackspacecount array
                    counter++;
                }
                else
                    parsedVersionTB.Text += text.Substring(text.Length - 1, 1); //add the new letter to the parsedVersionTB


                if (performedstrikethrough)
                {
                    performedstrikethrough = false;
                }


                for (int i = 0; i < counter; i++)
                {
                    parsedVersionTB.Select(oldcountbarray[i] - oldbackspacecount[i], oldbackspacecount[i]);
                    parsedVersionTB.SelectionFont = new Font(parsedVersionTB.SelectionFont, FontStyle.Strikeout);
                }
            }
            else if (backspaceflag == true)
            {
                int[] a_codes = DiffCharCodes(oldtext, true);
                int[] b_codes = DiffCharCodes(text, true);
                Diff.Item[] diffs = Diff.DiffInt(a_codes, b_codes);

                for (int n = 0; n < diffs.Length; n++)
                {
                    Diff.Item it = diffs[n];
                    if (it.deletedA > 0)
                    {
                        parsedVersionTB.Select(parsedVersionTB.Text.Length - BackspaceCount, BackspaceCount);
                        parsedVersionTB.SelectionFont = new Font(parsedVersionTB.SelectionFont, FontStyle.Strikeout);
                        performedstrikethrough = true;
                    }

                } // while

                oldcountb = parsedVersionTB.Text.Length;
                backspaceflag = false;
            }
        }

        /*
         * This method is called on KeyUp in the TypeHereTB and conducts actions to denote what should happen when the user stops typing
         */
        public void Stop_Typing(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Enter & typeHereTB.Text.Length == 0) | (conversation == false & disableGazeActs == true & question==false) | disabledTimers == true)
            {
                //do nothing because the typeHereTB is empty, gaze acts are disabled or the conversation hasn't begun yet
            }

            //if gaze acts are enabled do the following things
            if (disableGazeActs == false & disabledTimers == false & conversation==true)
            {
                Console.WriteLine("User has stop typing"); //denote state of the system
                /*
                 * Since the user stopped tying and it's still your turn or the conversation hasn't started then if the 
                 * stopTypingExtendedTimer and stopTypingExtendedTimerWarning timer hasn't been enabled 
                 * then it's done here
                 */
                if (stopTypingExendedTimer_going == false & (conversation == false | usersTurnOver==true))
                {
                    stopTypingExendedTimer.Enabled = true;
                    stopTypingExendedTimerWarning.Enabled = true;
                    stopTypingExendedTimer_going = true;
                }
            
                /* 
                 * Adds the event handlers for both the stopTypingExtendedTimer and 
                 * stopTypingExtendedTimerWarning timers
                 */
                if (stopTypingExendedTimer_handlerDefined == false & (conversation == false | usersTurnOver==true))
                {
                    stopTypingExendedTimer.Elapsed += stopTypingExendedTimerSet;
                    stopTypingExendedTimerWarning.Elapsed += stopTypingExendedTimerWarningSet;
                    stopTypingExendedTimer_handlerDefined = true;
                }

                /*
                 * Since the user stopped tying and it's still your turn or the conversation just started then if the 
                 * stopTypingTimer and stopTypingTimerWarning timer hasn't been enabled 
                 * then it's done here
                 */
                if (stopTypingTimer_going == false & (conversation == false | usersTurnOver==false))
                {
                    stopTypingTimer.Enabled = true;
                    stopTypingTimerWarning.Enabled = true;
                    stopTypingTimer_going = true;
                }

                /* 
                 * Adds the event handlers for both the stopTypingTimer and 
                 * stopTypingTimerWarning timers
                 */
                if (stopTypingTimer_handlerDefined == false & (question == false | usersTurnOver==false))
                {
                    stopTypingTimer.Elapsed += ScanGazeAct;
                    stopTypingTimerWarning.Elapsed += ScanGazeActWarn;
                    stopTypingTimer_handlerDefined = true;
                }
            }
        }

        /*
         * Method which occurs when stopTypingExtendedTimerWarning ticks. In this method the user is warned that the elapsed 
         * timer is going to be turned off since it's suggested that the user has stopped typing an are possibly considered 
         * to be idle.
         */
        public void stopTypingExendedTimerWarningSet(Object myObject, EventArgs myEventArgs)
        {
            Console.WriteLine("Elapse talking timer is about to be turned off");
            debugWindow.Text += "Elapsed talking timer is about to be turned off [" + DateTime.Now.ToString("hh:mm:ss") + "] \r\n";
            UpdateScrollOnTextBox(debugWindow);
        }

        /*
         * Method which occurs when stopTypingExtendedTimer ticks. In this method the elapsed timer is turned off since it's suggested 
         * that the user has stopped typing an are possibly considered to be idle. 
         */
        public void stopTypingExendedTimerSet(Object myObject, EventArgs myEventArgs)
        {

            elapsedTalkingTimer.Elapsed -= ConcurrenceGazeAct; //remove the event handler
            elapsedTalkingTimer_handlerDefined = false; //adjust variable since the event handler was removed
            Console.WriteLine("Elapsed talking timer has been turned off");
            debugWindow.Text += "Elapsed talking timer has been turned off [" + DateTime.Now.ToString("hh:mm:ss") + "] \r\n"; //notify the user of the timer being turned off
            UpdateScrollOnTextBox(debugWindow);//adjust the scroll on the debugWindow
            Update_label2(""); //clear the gaze act warning label
            elapsedTalkingTimer.Enabled = false; //disable the elapseTalkingTimer
            elapsedTalkingTimer_going = false; //adjust the variable since we turned off the timer

            if (usersTurnOver == true & fixate_occured==true)
            {
                inputWithTag = "CR>Fixate\r\n";
                gazeActsTB.Text += inputWithTag; //add the gaze act to the gaze act textbox
                gazeActsTB.Text += "—————\r\n";
                //usersTurnOver = true; 
            }
            
            if (usersTurnOver == true & fixate_occured == false)
            {
                inputWithTag = "CR>Fixate [" + DateTime.Now.ToString("hh:mm:ss") + "] \r\n";
                gazeActsTB.Text += inputWithTag; //add the gaze act to the gaze act textbox
                gazeActsTB.Text += "—————\r\n"; 
                command = "\"ACT\",\"implicit\",\"fixation\""; //create the command for the server client to do the confusion gaze act
                sbc.sendCommand(command); //send command to the server client
                debugWindow.AppendText("\nSent: " + command + "\r\n");
                //usersTurnOver = true;
                //update the scroll on the textboxes
                UpdateScrollOnTextBox(debugWindow);
                UpdateScrollOnRichTextBox(gazeActsTB);
            }
            
            elapsedResponseTimer.Enabled = true;
            elapsedResponseTimerWarning.Enabled = true;
            elapsedResponseTimer_going = true;
            Console.WriteLine("Elapse Response Timer now started");
            debugWindow.Text += "Elapse Response Timer now started [" + DateTime.Now.ToString("hh:mm:ss") + "].\r\n";
            UpdateScrollOnTextBox(debugWindow);
            elapsedResponseTimer.Elapsed += ConcurrenceGazeAct;
            elapsedResponseTimerWarning.Elapsed += concurrenceBehaviorWarn;
            elapsedResponseTimer_handlerDefined = true;
        }
/*
 * Gaze Act Functions
 */
        /*
         * Method for the fixate gaze act
         */
        public void FixateGazeAct()
        {
            concurrence_enabled = false; //set these variables to false since the fixate gaze act is going to be performed
            confusion_enabled = false;
            scan_enabled = false;
            fixate_initiated = true; //set this variable to true since the fixate gaze act is going to be performed
            conversation = true;
            /*
             * if a question hasn't been asked but scan currently happening then adjust the following
             */
            if (question == false)
            {
                Update_label(""); //clear the current gaze act 
                DelBackspaceCount = 0; //reset delete count   
            }

            /*
             * if a question has been asked adjust the following
             */
            else if (question == true)
            {
                Update_label(""); //clear current gaze act
            }
           
        }

        /*
         * Method for the scan gaze act
         */
        public void ScanGazeAct(Object myObject, EventArgs myEventArgs)
        {
            if (usersTurnOver == false)
            {
                Update_label("Scan (Short Glances)"); //set the current gaze act
                Update_label2(""); //clear warning gaze act label 
                Console.WriteLine("The scan gaze act now activated (for Ideal Elapse Time).");
                debugWindow.Text += "The scan gaze act now activated (for Ideal Elapse Time) [" + DateTime.Now.ToString("hh:mm:ss") + "]. \r\n";
                inputWithTag = "IET>Scan [" + DateTime.Now.ToString("hh:mm:ss") + "] \r\n";
                command = "\"ACT\",\"implicit\",\"scanning\""; //create the command to send to do the scan gaze act 
                sbc.sendCommand(command); //send command
                debugWindow.AppendText("\nSent: " + command + "\r\n");
                gazeActsTB.Text += inputWithTag; //add gaze act to the gaze act textbox
                UpdateScrollOnTextBox(debugWindow); //adjust the scroll on the debug window textbox
                UpdateScrollOnRichTextBox(gazeActsTB); //adjust the scroll on gaze act textbox
                scan_enabled = true; //scan is occuring
                AlphaCount = 0; //reset type count
                fixate_initiated = false; //fixate would need to occur again since scan is enabled
            }
            else
            {
                
            }
        }

        /*
         * Method for to warn that the scan gaze act is about to occur
         */
        public void ScanGazeActWarn(Object myObject, EventArgs myEventArgs)
        {
            if(usersTurnOver==false)
                Update_label2("Scan (Short Glances)"); //set gaze act warning label  
        }

        /*
         * Method for the concurrence gaze act
         */
        public void ConcurrenceGazeAct(Object myObject, EventArgs myEventArgs)
        {
            //check to see if delete count is over 5 and if confusion isn't currently enabled then do confusion gaze act instead
            if (DelBackspaceCount > 7 & confusion_enabled==false & usersTurnOver==false)
                ConfusionGazeAct();

                //if a question has been asked then tailor the tag because it's a response from the person on the other end and not the user
                else if (usersTurnOver == true)
                {
                    inputWithTag = "ERT>concurrence [" + DateTime.Now.ToString("hh:mm:ss") + "] \r\n";
                    gazeActsTB.Text += inputWithTag; //print the gaze act
                    String command = "\"ACT\",\"implicit\",\"concurrence\""; //create the command to send to the server client for the concurrence gaze act
                    sbc.sendCommand(command); //send the command to the server client
                    debugWindow.AppendText("\nSent: " + command + "\r\n");
                }
                //if a question hasn't been asked then it's considered to be concurrence on the user's end
                else
                {
                    /*
                    inputWithTag = "ETT>concurrence [" + DateTime.Now.ToString("hh:mm:ss") + "] \r\n";
                    gazeActsTB.Text += inputWithTag; //print the gaze act
                    command = "\"ACT\",\"implicit\",\"concurrence\""; //create the command to send to the server for the concurrence gaze act
                    sbc.sendCommand(command); //send the command to the server
                    debugWindow.AppendText("\nSent: " + command + "\r\n"); 
                     */ 
                }
                //update the scroll on the textboxes 
                UpdateScrollOnTextBox(debugWindow); 
                UpdateScrollOnRichTextBox(gazeActsTB);
                concurrence_enabled = true; //set this true because concurrence gaze act occuring
                confusion_enabled = false; //set this true because confusion is not occuring
        }

        /*
         * Method for the confusion gaze act
         */
        public void ConfusionGazeAct()
        {
            confusion_enabled = true; //set this true because confusion is occuring
            concurrence_enabled = false; //set this false because concurrence isn't occuring 
            
            Update_label("Confusion"); //update the current occuring gaze act
            Update_label2(""); //clear warning gaze act label 
            Console.WriteLine("The confusion gaze act is now activated\nDel Backspace Count is:{0}",DelBackspaceCount);
            debugWindow.Text += "The confusion gaze act is now activated [" + DateTime.Now.ToString("hh:mm:ss") + "].\r\n";
            inputWithTag = "ERtT>Confusion [" + DateTime.Now.ToString("hh:mm:ss") + "] \r\n";
            gazeActsTB.Text += inputWithTag; //add the gaze act to the gaze act textbox 
            command = "\"ACT\",\"implicit\",\"confusion\""; //create the command for the server client to do the confusion gaze act
            sbc.sendCommand(command); //send command to the server client
            debugWindow.AppendText("\nSent: " + command + "\r\n");
            //update the scroll on the textboxes
            UpdateScrollOnTextBox(debugWindow);
            UpdateScrollOnRichTextBox(gazeActsTB);
            //reset the typing count
            AlphaCount = 0;
            //fixate will need to occur since confusion is occuring 
            fixate_initiated = false;
        }

        /*
         * Method for the warning of concurrence gaze act
         */
        public void concurrenceBehaviorWarn(Object myObject, EventArgs myEventArgs) 
        {
            Update_label2("concurrence"); //set warning gaze act label 
        }

/*
 * Method to parse user input once they press enter and handel communication with the voice toolkit through the server client
 */
        /*
         * Method to parse the input to infer the gaze acts
         */
        public String ParseInput(string input)
        {
            //if tag is ::End, ::end, or any other case variation of the tag this will reset the conversation
            if (String.Compare(input, "::end", true) == 0)
            {
                //method to reset conversation
                ResetConversation();
                return "";
            }
            else
            {
                String textToSpeech = ""; //string to be sent to the voice toolkit via the server client
                double returnValue=0;
                Console.WriteLine("User input: {0}", input);
                
                //if gaze acts are disabled then just send the string to be spoken
                if (disableGazeActs)
                {
                    textToSpeech = input;
                }
                
                //if gaze acts are enabled then parse it for implicit gaze acts
                else if (disableGazeActs == false)
                {
                    String[] words = input.Split(' '); //split words of the text entered to the words array 
                    String[] sentanceArray = new String[100];
                    //int sentanceArrayCount = 0;
                    int wordCount = words.Length - 1; //get count of words
                    int sentanceNumber = 0;
                    if (!probability)
                    {
                        aversionProbability = 1.0;
                        fixationProbability = 1.0;
                        Console.WriteLine("avert %: " + aversionProbability + "fixate %: " + fixationProbability);
                    }
                    //int[] wordCoutPerSentance = new int[100];
                    //int wc = 0; 
                    if (words.Length == 1 & words[0].IndexOf("::") != -1)
                    {
                        //if only a tag was entered
                        textToSpeech += words[0];
                        textToSpeech += "::fixation";
                    }
                    else
                    {
                        textToSpeech = "::aversion "; //add aversion at the beginning of the interaction 
                        gazeActs[gaCounter] = "SS>Avert["; //add this to the gaze act array 
                        gaCounter++; //increment counter
                        

                        int startIndex = 0;
                        //goes through character by character of the input from left to right. 
                        for (int i = 0; i < input.Length - 1; i++)
                        {
                            //if a punctuation mark is found
                            if (input[i] == '.' | input[i] == '!' | input[i] == '?')
                            {
                                Console.WriteLine("punctuation found: " + input[i] + " at " + i);
                                sentanceNumber++; //sentance count in the input
                                //if it's not the first sentance do the following
                                if (sentanceNumber > 1)
                                {
                                    returnValue = randomValue.NextDouble(); //generate a random number
                                    Console.WriteLine("return value: " + returnValue); //prints the random number generated
                                    //if random number is between 0.0 and 1.0 is less than .7 (70% probablity) add the aversion to the beginning of the next sentance
                                    if (returnValue < aversionProbability)
                                    {
                                        textToSpeech += "::aversion ";
                                        gazeActs[gaCounter] = "SS>Avert["; //add this to the gaze act array 
                                        gaCounter++; //increment counter
                                    }
                                }

                                returnValue = randomValue.NextDouble(); //generate a random number
                                Console.WriteLine("random number: " + returnValue); //print random number
                                //appends sentance to string to be spoken
                                textToSpeech += input.Substring(startIndex, (i - startIndex) + 1) + " ";
                                //if random number between 0.0 and 1.0 and is less than .73 (73% probablity) add the fixation at the end of the sentance 
                                //(can be the the first sentance or a number of setances after)
                                if (returnValue < fixationProbability)
                                {
                                    textToSpeech += "::fixation ";
                                    gazeActs[gaCounter] = "ES>Fixate["; //add this to the gaze act array 
                                    gaCounter++; //increment counter
                                    fixate_occured = true;
                                }
                                //if not in less than .73 then don't add fixate
                                else
                                    fixate_occured = false; //fixate wasn't added 

                                //alter the beginning marker of the sentance to the most current sentance 
                                startIndex = i + 2;
                            }
                            else
                            { }
                        }

                        //if more than one sentance was entered in the interaction, but second sentance doens't have a punctuation ending.
                        if (startIndex != 0)
                        {
                            returnValue = randomValue.NextDouble();
                            Console.WriteLine("random number: " + returnValue);
                            if (returnValue < aversionProbability)
                            {
                                textToSpeech += "::aversion ";
                                gazeActs[gaCounter] = "SS>Avert["; //add this to the gaze act array
                                gaCounter++; //increment counter
                            }
                            textToSpeech += input.Substring(startIndex);

                            //add a fixate on a sentance that doesn't end in puntuation (could be only one sentance or multiple sentances, but it doesn't have punctuations on the last sentance
                            returnValue = randomValue.NextDouble();
                            Console.WriteLine("random number: " + returnValue);
                            if (returnValue < fixationProbability)
                            {
                                textToSpeech += " ::fixation ";
                                gazeActs[gaCounter] = "ES>Fixate["; //add this to the gaze act array 
                                gaCounter++; //increment counter
                                fixate_occured = true;
                            }
                            else
                                fixate_occured = false;

                        }
                        //if only one sentance was entered but it doesn't have punctuation in it.                            
                        else
                        {
                            textToSpeech += input;

                            //add a fixate on a sentance that doesn't end in puntuation (could be only one sentance or multiple sentances, but it doesn't have punctuations on the last sentance
                            returnValue = randomValue.NextDouble();
                            Console.WriteLine("random number: " + returnValue);
                            if (returnValue < fixationProbability)
                            {
                                textToSpeech += " ::fixation";
                                gazeActs[gaCounter] = "ES>Fixate["; //add this to the gaze act array 
                                gaCounter++; //increment counter
                                fixate_occured = true;
                            }
                            else
                                fixate_occured = false;

                        }

                        //adjust the gaze acts and add concurrence for longer sentances
                        textToSpeech = adjustText.adjustTextString(textToSpeech, words);
                        Console.WriteLine("After moving tags: " + textToSpeech); //print the altered string

                        DisableTimers(); //disable timers since it's about to be sent to be spoken

                        //create the command to send to the voice toolkit via the server client
                        textToSpeech = "\"SAY AND ACT\",\"typed\",\"" + voiceOptionsDropDown.SelectedItem + "\",\"" + speechRateSlider.Value + "\",\"1\",\"" + volumeLevelSlider.Value + "\",\"" + textToSpeech + "\"";
                        Console.WriteLine("Text Sent: " + textToSpeech);
                        NLPtoolkitText.Text += textToSpeech + "\r\n";
                        UpdateScrollOnTextBox(NLPtoolkitText);
                    }
                }
                return textToSpeech;
            }
        }

        /*
        * Method for messages recieved from voice toolkit server when sending commands
        */
        void sbc_DataIn(object sender, DataReceiveEventArgs e)
        {
            this.Invoke((MethodInvoker)delegate
            {
                this.debugWindow.AppendText("\nReceived: " + e.message + "\r\n");
                UpdateScrollOnTextBox(debugWindow);
                Console.WriteLine("Received: " + e.message);
                reply.Enqueue(e.message); //enqueue messages in the reply queue
                while (reply.Count != 0)
                {
                    String r = reply.Dequeue().ToString();
                    AddTimeStampsAndPrint(e.message); //send to method which categorizes the reply messages 
                }
            });

        }

        /*
         * Method for categorizing the various replies from the voice toolkit commands sent through the server client 
         */
        public void AddTimeStampsAndPrint(string returnMessage)
        {
            //if id tag for reply was a request for voices parse avaliable voices so that it only includes the names 
            if (returnMessage.IndexOf("voices") != -1 & returnMessage.Length > 8)
            {
                string[] stringSeparators = new string[] { "\",\"" };
                String[] voiceAttributes = returnMessage.Split(stringSeparators, StringSplitOptions.None);
                String Names;
                voiceAttributes[0] = voiceAttributes[0].Substring(9);
                Console.WriteLine("Voices found on your computer: ");
                foreach (String va in voiceAttributes)
                    Console.WriteLine(va);
                int i = 0;

                while (i <= voiceAttributes.Length - 1)
                {
                    Names = voiceAttributes[i];
                    i = i + 6;
                    voiceOptionsDropDown.Items.Add(Names); //add voice names to the dropdown list
                }
                voiceOptionsDropDown.SelectedIndex = 0; //select first option by default
            }
            //if id tag was an implicit event (scan, conccurrence (typing), confused)
            else if (returnMessage.IndexOf("implicit") != -1)
            {
                //don't print or do any additional actions 
            }

            //if home position for Survivor Buddy was called because gaze acts were disabled do the following actions 
            else if (returnMessage.IndexOf("disableGazeActs") != -1)
            {
                disableGazeActs = true;
                DisableTimers(); //disable the timers
                //notify the user that the gaze acts have been disabled 
                gazeActsTB.Text += "Gaze acts disabled [" + DateTime.Now.ToString("hh:mm:ss") + "] \r\n";
                gazeActsTB.Text += "—————\r\n";
                UpdateScrollOnRichTextBox(gazeActsTB);
                debugWindow.Text += "Gaze acts disabled\r\n";
                UpdateScrollOnTextBox(debugWindow);
            }
            //if text was submitted to the voice toolkit do the following items
            else if (returnMessage.IndexOf("typed") != -1)
            {
                gaCounter = 0;
                int comma = returnMessage.IndexOf("\",\"");
                int secondComma = returnMessage.IndexOf("\",\"", comma + 1);

                if (secondComma == -1)
                {
                    if (returnMessage.IndexOf("Said") != -1)
                    {
                        start = 0;
                        UpdateScrollOnRichTextBox(gazeActsTB);
                        
                        //restart stop_typing timers to detect idle state
                        speechCompleted();                       
                        //if question was asked then allow response timers to tick 
                        if (question == true)
                            StartElapseResponseTimer();
                    }
                }
                //gaze acts action performed
                else
                {
                    String textToBePrinted = "";
                    String gazeAct = returnMessage.Substring(comma + 3, (secondComma - comma) - 3);
                    Console.WriteLine("gazeAct: " + gazeAct);
                    String timeStamp = returnMessage.Substring(secondComma + 3, 8);
                    Console.WriteLine("time stamp: " + timeStamp);
                    debugWindow.AppendText("Gaze Act found: " + gazeAct + "\r\n");
                    UpdateScrollOnTextBox(debugWindow);

                    if (String.Compare(gazeAct, "fixation", true) == 0)
                    {
                        gazeActsTB.Text += gazeActs[start] + timeStamp + "]\r\n";
                        start++;
                    }
                    else if (String.Compare(gazeAct, "aversion", true) == 0)
                    {
                        gazeActsTB.Text += gazeActs[start] + timeStamp + "]\r\n";
                        start++;
                    }
                    else if (String.Compare(gazeAct, "concurrence", true) == 0)
                    {
                        textToBePrinted = "ETT>" + gazeAct + "[" + timeStamp + "]\r\n";
                        gazeActsTB.Text += textToBePrinted;
                    }

                    else
                    {
                        textToBePrinted = "EB>" + gazeAct + "[" + timeStamp + "]\r\n";
                        gazeActsTB.Text += textToBePrinted;
                    }
                    UpdateScrollOnRichTextBox(gazeActsTB);
                }
            }
        }

/*
 * Methods to manipulate and manage timers for various cases
 */
        /*
         * Method to start the elapse response timers after a question has been submitted and asked
         */
        public void StartElapseResponseTimer()
        {
            Console.WriteLine("User's turn is over");
            debugWindow.Text += "User's turn is over [" + DateTime.Now.ToString("hh:mm:ss") + "] .\r\n";
            UpdateScrollOnTextBox(debugWindow);

            if (stopTypingTimer_handlerDefined == true)
            {
                stopTypingTimer.Elapsed -= ScanGazeAct;
                stopTypingTimerWarning.Elapsed -= ScanGazeActWarn;
                stopTypingTimer_handlerDefined = false;
            }
            if (stopTypingTimer_going == true)
            {
                stopTypingTimer.Enabled = false;
                stopTypingTimerWarning.Enabled = false;
                stopTypingTimer_going = false;
            }
            if (stopTypingExendedTimer_handlerDefined == true)
            {
                stopTypingExendedTimer.Elapsed -= ConcurrenceGazeAct;
                stopTypingExendedTimerWarning.Elapsed -= ConcurrenceGazeAct;
                stopTypingExendedTimer_handlerDefined = false;
            }
            if (stopTypingExendedTimer_going == true)
            {
                stopTypingExendedTimer.Enabled = false;
                stopTypingExendedTimerWarning.Enabled = false;
                stopTypingExendedTimer_going = false;
            }

            if (elapsedResponseTimer_going == false)
            {
                elapsedResponseTimer.Enabled = true;
                elapsedResponseTimerWarning.Enabled = true;
                elapsedResponseTimer_going = true;
                Console.WriteLine("Elapse Response Timer now started");
                debugWindow.Text += "Elapse Response Timer now started [" + DateTime.Now.ToString("hh:mm:ss") + "].\r\n";
                UpdateScrollOnTextBox(debugWindow);
            }
            if (elapsedResponseTimer_handlerDefined == false)
            {
                elapsedResponseTimer.Elapsed += ConcurrenceGazeAct;
                elapsedResponseTimerWarning.Elapsed += concurrenceBehaviorWarn;
                elapsedResponseTimer_handlerDefined = true;
            }
        }

        /*
         * Disables timers 
         */
        private void DisableTimers()
        {
            disabledTimers = true;

            stopTypingTimer.Elapsed -= ScanGazeAct; //remove the event handler for the stopTypingTimer
            stopTypingTimerWarning.Elapsed -= ScanGazeActWarn; //remove the event handler for the stopTypingTimerWarning 
            stopTypingTimer_handlerDefined = false; //set this variable to false sine we removed the event handlers
            stopTypingTimer.Enabled = false; //disable the stopTypingTimer  
            stopTypingTimerWarning.Enabled = false; //disable the stopTypingTimerWarning since the user turn has ended
            stopTypingTimer_going = false; //set this variable to false sense we disabled the timers

            /*
              * if the stopTypingExendedTimer timer is going disable the timer, and disable the stopTypingExendedTimerWarning timer since the coversation has 
              * started or continued.
              */
            if (stopTypingExendedTimer_going == true)
            {
                stopTypingExendedTimer.Enabled = false;
                stopTypingExendedTimerWarning.Enabled = false;
                stopTypingExendedTimer_going = false;
                Console.WriteLine("Stop Typing extended has been cancelled");
            }

            /*
             * remove the event handler from the stopTypingExendedTimer timer and the stopTypingExtendedTimerWarning timer since the timer has been disabled. 
             */
            if (stopTypingExendedTimer_handlerDefined == true)
            {
                stopTypingExendedTimer.Elapsed -= stopTypingExendedTimerSet;
                stopTypingExendedTimerWarning.Elapsed -= stopTypingExendedTimerWarningSet;
                stopTypingExendedTimer_handlerDefined = false;
            }

            /*
             * if elapsedResponseTimer is going then disable the elapseResponseTimer and the elapsedResponseTimerWarning since the conversation 
             * has started. Adjust fixate_initiated so that it doesn't occur again. 
             */
            if (elapsedResponseTimer_going == true)
            {
                elapsedResponseTimer.Enabled = false;
                elapsedResponseTimerWarning.Enabled = false;
                elapsedResponseTimer_going = false;
                fixate_initiated = false;
            }

            /*
             * Remove the elapsedResponseTimer and elapsedResponseTimerWarning event handlers since the elapseResponseTimer and elapseResponseTimerWarning 
             * has been disabled
             */
            if (elapsedResponseTimer_handlerDefined == true)
            {
                elapsedResponseTimer.Elapsed -= ConcurrenceGazeAct;
                elapsedResponseTimerWarning.Elapsed -= concurrenceBehaviorWarn;
                elapsedResponseTimer_handlerDefined = false;
            }

            debugWindow.Text += "Disabled timers at time: " + DateTime.Now.ToString("hh:mm:ss") + "\r\n";
            UpdateScrollOnTextBox(debugWindow);
        }

        /*
         * Method to restart stop_typing timers since the speech is done being said
         */
        private void speechCompleted()
        {
            
            if (disableGazeActs == false & disabledTimers == true)
            {
                disabledTimers = false;
                Console.WriteLine("Speech entered has been spoken"); //denote state of the system
                debugWindow.Text += "Speech entered has been spoken at time: " + DateTime.Now.ToString("hh:mm:ss") + "\r\n";
                UpdateScrollOnTextBox(debugWindow);

                /*
                 * Since the user stopped tying and a question hasn't been submitted then if the 
                 * stopTypingExtendedTimer and stopTypingExtendedTimerWarning timer hasn't been enabled 
                 * then it's done here
                 */
                if (stopTypingExendedTimer_going == false & usersTurnOver == true)
                {
                    Console.WriteLine("Elapse response timer should be turned on now.");
                    stopTypingExendedTimer.Enabled = true;
                    stopTypingExendedTimerWarning.Enabled = true;
                    stopTypingExendedTimer_going = true;
                }
                /* 
                 * Adds the event handlers for both the stopTypingExtendedTimer and 
                 * stopTypingExtendedTimerWarning timers
                 */
                if (stopTypingExendedTimer_handlerDefined == false & usersTurnOver == true)
                {
                    stopTypingExendedTimer.Elapsed += stopTypingExendedTimerSet;
                    stopTypingExendedTimerWarning.Elapsed += stopTypingExendedTimerWarningSet;
                    stopTypingExendedTimer_handlerDefined = true;
                }

                /*
                 * Since the user stopped tying and a question hasn't been submitted then if the 
                 * stopTypingTimer and stopTypingTimerWarning timer hasn't been enabled 
                 * then it's done here
                 */
                if (stopTypingTimer_going == false & usersTurnOver == false)
                {
                    stopTypingTimer.Enabled = true;
                    stopTypingTimerWarning.Enabled = true;
                    stopTypingTimer_going = true;
                }

                /* 
                 * Adds the event handlers for both the stopTypingTimer and 
                 * stopTypingTimerWarning timers
                 */
                if (stopTypingTimer_handlerDefined == false & usersTurnOver == false)
                {
                    stopTypingTimer.Elapsed += ScanGazeAct;
                    stopTypingTimerWarning.Elapsed += ScanGazeActWarn;
                    stopTypingTimer_handlerDefined = true;
                }
            }
        }

/*
 * Methods to manage and manipulate the components on the form 
 */
        //updates the scroll on the rich textboxes on the form
        public void UpdateScrollOnRichTextBox(RichTextBox textBox)
        {
            textBox.SelectionStart = textBox.Text.Length;
            textBox.ScrollToCaret();
            textBox.Refresh();
        }

        public void UpdateScrollOnTextBox(TextBox textBox)
        {
            textBox.SelectionStart = textBox.Text.Length;
            textBox.ScrollToCaret();
            textBox.Refresh();
        }

        //updating a labels for warning gaze act and occuring gaze act 
        delegate void updateLabelTextDelegate(string newText);
        private void Update_label(string newText)
        {
            if (OccuringGazeAct.InvokeRequired)
            {
                updateLabelTextDelegate del = new updateLabelTextDelegate(Update_label);
                OccuringGazeAct.Invoke(del, new object[] {newText});
            }
            else
            {
                OccuringGazeAct.Text = newText;
            }
            
        }
        
        private void Update_label2(string newText)
        {
            if (gazeActWarningTB.InvokeRequired)
            {
                // this is worker thread
                updateLabelTextDelegate del = new updateLabelTextDelegate(Update_label);
                gazeActWarningTB.Invoke(del, new object[] { newText });
            }
            else
            {
                // this is UI thread
                gazeActWarningTB.Text = newText;
            }
        }

        //notifies user of location of sliders 
        private void volumeLevel_Scroll(object sender, System.EventArgs e)
        {
            // Display the trackbar value in the text box.
            currentVolumeLevel.Text = volumeLevelSlider.Value.ToString();
        }

        private void speechRate_Scroll(object sender, System.EventArgs e)
        {
            // Display the trackbar value in the text box.
            currentSpeechRate.Text = speechRateSlider.Value.ToString();
        }

        //hides the extra information or displays extra inforamtion on the form 
        private void onClick(object sender, EventArgs e)
        {
            if (extendedInfo % 2 == 0)
            {
                button1.Text = "View Details";
                this.Height = 545;
                typeHereTB.Focus();   
            }
            else
            {
                button1.Text = "Hide Details";
                this.Height = 690;
                typeHereTB.Focus();
            }
            extendedInfo++;
        }
        
        //sends command to put Survivor Buddy in the resting position to store or close
        private void StoreSBButton_Click(object obj, EventArgs et)
        {
            if (SBstored == false)
            {
                End();
                SBstored = true;
            }
        }
      /*
       * Method to start a new conversation (resets form) and puts Survivor Buddy in home position
       */
       private void HomeClick(object sender, EventArgs e)
       {
          if(SBstored==false)
           ResetConversation();
       }

       /*
        * Starts Survivor Buddy (opens and raises Survivor Buddy)
        */
       private void StartSBClick(object sender, EventArgs e)
       {
           if (SBstored == true)
           {
               sbc.sendCommand("\"ACT\",\"openuid\",\"open\"");
               SBstored = false;
               startButton.Visible = false;
               storeButton.Visible = true;
               typeHereTB.Enabled = true;
           }
       }

       /*
        * Method called when clicking Disable_Enable button to enable or disable gaze acts 
        */
       private void Enable_DisableGazeActs(object sender, EventArgs e)
       {
           EnableDisableGazeActs_actions();
       }
        //Enable_Disable gaze act actions 
       private void EnableDisableGazeActs_actions()
       {
           if (gazeActsTB.Enabled)
           {
               sbc.sendCommand("\"ACT\",\"disableGazeActs\",\"home\"");
               DisableEnableGA.Text = "Enable Gaze Acts";
               DisableEnableGA.ForeColor = Color.Green;
               currentGazeActStatus.ForeColor = Color.Red;
               currentGazeActStatus.Text = "Disabled";
               probabilityState.ForeColor = Color.Red;
               probabilityState.Text = "Disabled";
               probability = false;
               gazeActsTB.Enabled = false;
               gaWLbDes.Visible = false;
               gazeActWarningTB.Visible = false;
               gaLDes.Visible = false;
               OccuringGazeAct.Visible = false;
               typeHereTB.Focus();
           }
           else
           {
               DisableEnableGA.Text = "Disable Gaze Acts";
               currentGazeActStatus.Text = "Enabled";
               currentGazeActStatus.ForeColor = Color.Green;
               probabilityState.ForeColor = Color.Green;
               probabilityState.Text = "Enabled";
               probability = true;
               DisableEnableGA.ForeColor = Color.Red;
               disableGazeActs = false;
               gazeActsTB.Text += "Gaze acts enabled [" + DateTime.Now.ToString("hh:mm:ss") + "] \r\n";
               gazeActsTB.Text += "—————\r\n";
               UpdateScrollOnRichTextBox(gazeActsTB);
               debugWindow.Text += "Gaze acts enabled\r\n";
               UpdateScrollOnTextBox(debugWindow);
               gazeActsTB.Enabled = true;
               gaWLbDes.Visible = true;
               gazeActWarningTB.Visible = true;
               gaLDes.Visible = true;
               OccuringGazeAct.Visible = true;
               typeHereTB.Focus();
           }
       }
       private void DisableEnableProbability_Click(object sender, EventArgs e)
       {
           if (probability)
           {
               DisableEnableProbability.Text = "Enable Probability";
               DisableEnableProbability.ForeColor = Color.Green;
               probabilityState.ForeColor = Color.Red;
               probabilityState.Text = "Disabled";
               probability = false;
           }
           else
           {
               DisableEnableProbability.Text = "Disable Probability";
               DisableEnableProbability.ForeColor = Color.Red;
               probabilityState.ForeColor = Color.Green;
               probabilityState.Text = "Enabled";
               probability = true;            
           }
       }

       /*
        * Method to reset/start a new conversation 
        */
       private void ResetConversation()
       {
           sbc.sendCommand("\"ACT\",\"homeuid\",\"home\"");
           typeHereTB.Clear();
           gazeActsTB.Clear();
           parsedVersionTB.Clear();
           NLPtoolkitText.Clear();
           stopTypingTimer.Elapsed -= ScanGazeAct;
           stopTypingTimer.Enabled = false;

           elapsedTalkingTimer.Elapsed -= ConcurrenceGazeAct;
           elapsedTalkingTimer.Enabled = false;

           elapsedResponseTimer.Elapsed -= ConcurrenceGazeAct;
           elapsedResponseTimer.Enabled = false;

           stopTypingExendedTimer.Elapsed -= stopTypingExendedTimerSet;
           stopTypingExendedTimer.Enabled = false;

           stopTypingTimerWarning.Elapsed -= ScanGazeActWarn;
           stopTypingTimerWarning.Enabled = false;

           elapsedResponseTimerWarning.Elapsed -= concurrenceBehaviorWarn;
           elapsedResponseTimerWarning.Enabled = false;

           stopTypingExendedTimerWarning.Elapsed -= stopTypingExendedTimerWarningSet;
           stopTypingExendedTimerWarning.Enabled = false;

           stopTypingTimer_handlerDefined = false;
           stopTypingTimer_going = false;
           elapsedTalkingTimer_handlerDefined = false;
           elapsedTalkingTimer_going = false;
           elapsedResponseTimer_handlerDefined = false;
           elapsedResponseTimer_going = false;
           stopTypingExendedTimer_handlerDefined = false;
           stopTypingExendedTimer_going = false;
           question = false;
           conversation = false;
           fixate_initiated = false;
           concurrence_enabled = false;
           confusion_enabled = false;
           scan_enabled = false;
         
           typeHereTB.Clear();
           typeHereTB.Refresh();
           gazeActsTB.Clear();
           gazeActsTB.Refresh();
           debugWindow.Clear();
           debugWindow.Refresh();
           parsedVersionTB.Clear();
           parsedVersionTB.Refresh();
           Update_label("");
           Update_label2("");
           typeHereTB.Focus();

           DelBackspaceCount = 0; //variable to count how many times the user presses the backspace or delete key
           AlphaCount = 0; //variable to count how many characters the user types to make up a conversation statment 
           extendedInfo = 0; //variable to determine whether the information portion of the application is visible or not

           inputWithTag = ""; //appends entered text with appropriate tags
           startTime = ""; //string that stores the time the user starts talking 
           endTime = ""; //string that stores the time the user presses enter and submits the text

           differance = new Diff();
           oldtext = " ";
           backspaceflag = false;
           oldcountbarray = new int[10000];
           oldbackspacecount = new int[10000];
           oldcountb = 0;
           BackspaceCount = 0;
           counter = 0;
           text = "";
           firstime = true;
           performedstrikethrough = false;
           gazeActs = new String[100];
           gaCounter = 0;
           explicitBehaviors = new String[100];
           reply = new Queue();
           start = 0;
           command = "";
           disabledTimers = false;
       }

       /*
        * Actions done when storing Survivor Buddy
        */
       public void End()
       {
           if (conversation == true | scan_enabled == true | stopTypingExendedTimer_going == true | fixate_initiated == true)
           {
               Console.WriteLine("Conversation over");

               if (SBstored == false)
               {
                   sbc.sendCommand("\"ACT\",\"closeuid\",\"close\"");
                   SBstored = true;
                   startButton.Visible = true;
                   storeButton.Visible = false;
                   typeHereTB.Enabled = false;
               }
               stopTypingTimer.Elapsed -= ScanGazeAct;
               stopTypingTimer.Enabled = false;

               elapsedTalkingTimer.Elapsed -= ConcurrenceGazeAct;
               elapsedTalkingTimer.Enabled = false;

               elapsedResponseTimer.Elapsed -= ConcurrenceGazeAct;
               elapsedResponseTimer.Enabled = false;

               stopTypingExendedTimer.Elapsed -= stopTypingExendedTimerSet;
               stopTypingExendedTimer.Enabled = false;

               stopTypingTimerWarning.Elapsed -= ScanGazeActWarn;
               stopTypingTimerWarning.Enabled = false;

               elapsedResponseTimerWarning.Elapsed -= concurrenceBehaviorWarn;
               elapsedResponseTimerWarning.Enabled = false;

               stopTypingExendedTimerWarning.Elapsed -= stopTypingExendedTimerWarningSet;
               stopTypingExendedTimerWarning.Enabled = false;

               stopTypingTimer_handlerDefined = false;
               stopTypingTimer_going = false;
               elapsedTalkingTimer_handlerDefined = false;
               elapsedTalkingTimer_going = false;
               elapsedResponseTimer_handlerDefined = false;
               elapsedResponseTimer_going = false;
               stopTypingExendedTimer_handlerDefined = false;
               stopTypingExendedTimer_going = false;
               question = false;
               conversation = false;
               fixate_initiated = false;
               concurrence_enabled = false;
               confusion_enabled = false;
               scan_enabled = false;
               startTime = "";
               endTime = "";

               typeHereTB.Clear();
               typeHereTB.Refresh();
               gazeActsTB.Clear();
               gazeActsTB.Refresh();
               debugWindow.Clear();
               debugWindow.Refresh();
               parsedVersionTB.Clear();
               parsedVersionTB.Refresh();
               Update_label("");
               Update_label2("");
               typeHereTB.Focus();

               DelBackspaceCount = 0; //variable to count how many times the user presses the backspace or delete key
               AlphaCount = 0; //variable to count how many characters the user types to make up a conversation statment 
               extendedInfo = 0; //variable to determine whether the information portion of the application is visible or not

               inputWithTag = ""; //appends entered text with appropriate tags
               startTime = ""; //string that stores the time the user starts talking 
               endTime = ""; //string that stores the time the user presses enter and submits the text

               differance = new Diff();
               oldtext = " ";
               backspaceflag = false;
               oldcountbarray = new int[10000];
               oldbackspacecount = new int[10000];
               oldcountb = 0;
               BackspaceCount = 0;
               counter = 0;
               text = "";
               firstime = true;
               performedstrikethrough = false;
               gazeActs = new String[100];
               gaCounter = 0;
               explicitBehaviors = new String[100];
               reply = new Queue();
               start = 0;
               command = "";
               disabledTimers = false;
           }
           if (SBstored == false & conversation == false)
           {
               sbc.sendCommand("\"ACT\",\"closeuid\",\"close\"");
               SBstored = true;
               startButton.Visible = true;
               storeButton.Visible = false;
               typeHereTB.Enabled = false;
           }
       }

       //method used in strike-through method 
       private static int[] DiffCharCodes(string aText, bool ignoreCase)
       {
           int[] Codes;

           if (ignoreCase)
               aText = aText.ToUpperInvariant();

           Codes = new int[aText.Length];

           for (int n = 0; n < aText.Length; n++)
               Codes[n] = (int)aText[n];

           return (Codes);
       }

       //Method to exit program occurs on elapse time (30 seconds)      
       public void ExitProgram(Object myObject, EventArgs myEventArgs)
        {
            Application.Exit();
        }
                       
    }
}
